Skip to content

DOC: update the pandas.DataFrame.pct_change docstring #20310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

myles
Copy link
Contributor

@myles myles commented Mar 12, 2018

Checklist for the pandas documentation sprint (ignore this if you are doing
an unrelated PR):

  • PR title is "DOC: update the docstring"
  • The validation script passes: scripts/validate_docstrings.py <your-function-or-method>
  • The PEP8 style check passes: git diff upstream/master -u -- "*.py" | flake8 --diff
  • The html version looks good: python doc/make.py --single <your-function-or-method>
  • It has been proofread on language by another sprint participant

Please include the output of the validation script below between the "```" ticks:

################################################################################
################### Docstring (pandas.DataFrame.pct_change)  ###################
################################################################################

Percentage change between the current and previous element.

This is useful in comparing the percentage of change in a time series
of elements.

Parameters
----------
periods : int, default 1
    Periods to shift for forming percent change.
fill_method : str, default 'pad'
    How to handle NAs before computing percent changes.
limit : int, default None
    The number of consecutive NAs to fill before stopping.
freq : DateOffset, timedelta, or offset alias string, optional
    Increment to use from time series API (e.g. 'M' or BDay()).
**kwargs : mapping, optional
    Additional keyword arguments are passed into
    `DataFrame.shift` or `Series.shift`.

Returns
-------
chg : Series or DataFrame
    The same type as the calling object.

Examples
--------
See the percentage change in a Series.

>>> s = pd.Series([90, 91, 85])
>>> s
0    90
1    91
2    85
dtype: int64
>>> s.pct_change()
0         NaN
1    0.011111
2   -0.065934
dtype: float64

See the percentage change in a Series where filling NAs with last
valid observation forward to next valid.

>>> s = pd.Series([90, 91, None, 85])
>>> s
0    90.0
1    91.0
2     NaN
3    85.0
dtype: float64
>>> s.pct_change(fill_method='ffill')
0         NaN
1    0.011111
2    0.000000
3   -0.065934
dtype: float64

Percentage change in French franc, Deutsche Mark, and Italian lira from
1 January 1980 to 1 March 1980.

>>> df = pd.DataFrame({
...     'FR': [4.0405, 4.0963, 4.3149],
...     'GR': [1.7246, 1.7482, 1.8519],
...     'IT': [804.74, 810.01, 860.13]},
...     index=['1980-01-01', '1980-02-01', '1980-03-01'])
>>> df
                FR      GR      IT
1980-01-01  4.0405  1.7246  804.74
1980-02-01  4.0963  1.7482  810.01
1980-03-01  4.3149  1.8519  860.13
>>> df.pct_change()
                  FR        GR        IT
1980-01-01       NaN       NaN       NaN
1980-02-01  0.013810  0.013684  0.006549
1980-03-01  0.053365  0.059318  0.061876

Percentage of change in GOOG and APPL stock volume.

>>> df = pd.DataFrame({
...     '2016': [1769950, 30586265],
...     '2015': [1500923, 40912316],
...     '2014': [1371819, 41403351]},
...     index=['GOOG', 'APPL'])
>>> df
          2016      2015      2014
GOOG   1769950   1500923   1371819
APPL  30586265  40912316  41403351
>>> df.pct_change(axis='columns')
      2016      2015      2014
GOOG   NaN -0.151997 -0.086016
APPL   NaN  0.337604  0.012002

See Also
--------
DataFrame.diff : see the difference of two columns
Series.diff : see the difference of two columns

################################################################################
################################## Validation ##################################
################################################################################

Errors found:
	Errors in parameters section
		Parameters {'kwargs'} not documented
		Unknown parameters {'**kwargs'}

Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice PR! Added a few comments

freq : DateOffset, timedelta, or offset alias string, optional
Increment to use from time series API (e.g. 'M' or BDay())
Increment to use from time series API (e.g. 'M' or BDay()).
kwargs : mapping, optional
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kwargs : mapping, optional -> **kwargs

Increment to use from time series API (e.g. 'M' or BDay())
Increment to use from time series API (e.g. 'M' or BDay()).
kwargs : mapping, optional
A dictionary of keyword arguments passed into
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a dictionary, but just additional keywords, so you cna make this ""Additional keyword arguments are passed .."

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But on second thought, are there actually additional keywords for shift?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what it's mapped to

pandas/pandas/core/generic.py

Lines 7561 to 7562 in 7169830

rs = (data.div(data.shift(periods=periods, freq=freq, axis=axis,
**kwargs)) - 1)


Returns
-------
chg : %(klass)s
chg : Series or DataFrame, same type as the input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'input' -> 'calling object'

See Also
--------
pandas.DataFrame.diff : see the difference of two columns
pandas.Series.diff : see the difference of two columns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this above the "Examples" section?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also remove the pandas. prefix I think.

@TomAugspurger TomAugspurger added this to the 0.23.0 milestone Mar 12, 2018
freq : DateOffset, timedelta, or offset alias string, optional
Increment to use from time series API (e.g. 'M' or BDay())
Increment to use from time series API (e.g. 'M' or BDay()).
**kwargs : mapping, optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove the type here. Just **kwargs.

Increment to use from time series API (e.g. 'M' or BDay()).
**kwargs : mapping, optional
Additional keyword arguments are passed into
``DataFrame.shift``/``Series.shift``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single backticks. "or" instead of /


Returns
-------
chg : %(klass)s
chg : Series or DataFrame, same type as the calling object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change : Series or DataFrame
    The same type as the calling object.


Notes
-----

By default, the percentage change is calculated along the stat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think most users will understand stat access. Best to remove this and only focus on Series / DataFrame.

By default, the percentage change is calculated along the stat
axis: 0, or ``Index``, for ``DataFrame`` and 1, or ``minor`` for
``Panel``. You can change this with the ``axis`` keyword argument.

Percentage change in French franc, Deutsche Mark, and Italian lira from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move under the "Examples" header.

1980-01-01 NaN NaN NaN
1980-02-01 0.013810 0.013684 0.006549
1980-03-01 0.053365 0.059318 0.061876

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show an example with axis='columns'. Can use the same DataFrame, even though it's a silly example for axis=1.

See Also
--------
pandas.DataFrame.diff : see the difference of two columns
pandas.Series.diff : see the difference of two columns
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also remove the pandas. prefix I think.

@codecov
Copy link

codecov bot commented Mar 12, 2018

Codecov Report

Merging #20310 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #20310      +/-   ##
==========================================
+ Coverage    91.7%   91.72%   +0.02%     
==========================================
  Files         150      150              
  Lines       49165    49165              
==========================================
+ Hits        45087    45099      +12     
+ Misses       4078     4066      -12
Flag Coverage Δ
#multiple 90.11% <100%> (+0.02%) ⬆️
#single 41.86% <0%> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/generic.py 95.85% <100%> (ø) ⬆️
pandas/plotting/_converter.py 66.81% <0%> (+1.73%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7169830...a0dcac2. Read the comment docs.

Note that it's immedate, but configurable.

no type on kwargs.

Example with periods.

Moved See Also.
@TomAugspurger
Copy link
Contributor

Made some touchups. Thanks @myles!

@TomAugspurger TomAugspurger merged commit c76120f into pandas-dev:master Mar 12, 2018
@jorisvandenbossche
Copy link
Member

@TomAugspurger small comment on when merging: can you remove all the different commit messages (in case of those DOC PRs it's mostly useless to keep them). It's maybe not that important and only my nitpicky self, but it's only a "click in box, ctrl-A, del" combo away :-)

@TomAugspurger
Copy link
Contributor

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants